package com.jane.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.google.gson.Gson;
import com.jane.adapter.CategoryAdapter;
import com.jane.bean.CategoryBean;
import com.jane.config.NetConfig;
import com.jane.gzzb.InnerCategoryActivity;
import com.jane.gzzb.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

public class CategoryFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener {
    private GridView gridView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private ArrayList<CategoryBean.DataListBean> list=new ArrayList<>();
    private CategoryAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_category, container, false);
        initView(view);
        initData();
        setListener();
        return view;
    }

    private void initView(View view) {
        gridView= (GridView) view.findViewById(R.id.gridview_category);
        swipeRefreshLayout= (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout_category);
        swipeRefreshLayout.setColorSchemeColors(Color.parseColor("#3BA9F2"),Color.parseColor("#FFFF26"),Color.parseColor("#FF0000"),Color.parseColor("#FF80C0"));
        adapter=new CategoryAdapter(list,getActivity());
        gridView.setAdapter(adapter);
    }

    private void initData() {
        RequestParams params=new RequestParams(NetConfig.CATEGORY_PATH);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                swipeRefreshLayout.setRefreshing(false);
                //添加数据源
                list.clear();
                CategoryBean categoryBean=new Gson().fromJson(result,CategoryBean.class);
                list.addAll(categoryBean.getData_list());
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                swipeRefreshLayout.setRefreshing(false);
            }
            @Override
            public void onCancelled(CancelledException cex) {
                swipeRefreshLayout.setRefreshing(false);
            }
            @Override
            public void onFinished() {
                adapter.notifyDataSetChanged();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }
    private void setListener() {
        swipeRefreshLayout.setOnRefreshListener(this);
        gridView.setOnItemClickListener(this);
    }
    //设置下拉刷新的监听
    @Override
    public void onRefresh() {
        initData();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //TODO 创建二级界面, 进行跳转
        Intent intent=new Intent(getActivity(), InnerCategoryActivity.class);
        intent.putExtra("gameName",list.get(position).getName());
        startActivity(intent);
    }
}
